package com.stek.app;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.stek.R;
import com.stek.model.AppInfo;
import com.stek.utils.Utils;

public class AppListAdapter extends BaseAdapter {

	private Context context;
	private ArrayList<AppInfo> applications;
	private OnInnerClickLisener lisener;

	public AppListAdapter(Context context, ArrayList<AppInfo> applications) {
		super();
		this.context = context;
		this.applications = applications;
	}

	@Override
	public int getCount() {
		return applications.size();
	}

	@Override
	public Object getItem(int position) {
		return applications.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		Holder holder;
		if (null == convertView) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.row_common3, null);
			holder = new Holder();
			holder.tvName = (TextView) convertView.findViewWithTag(context
					.getString(R.string.tag_name));
			holder.tvDesc = (TextView) convertView.findViewWithTag(context
					.getString(R.string.tag_desc));
			holder.imgMore = (ImageView) convertView
					.findViewById(R.id.img_more);
			holder.imgIcon = (ImageView) convertView
					.findViewById(R.id.img_icon);
			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}
		AppInfo application = applications.get(position);
		holder.tvName.setText(application.name);
		if (application.isSystemApp) {
			holder.tvDesc.setVisibility(View.VISIBLE);
		}else {
			holder.tvDesc.setVisibility(View.GONE);
		}
		holder.imgIcon.setImageDrawable(application.icon);
		holder.imgMore.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (lisener != null) {
					lisener.onInnerClickLisener(v, position);
				}

			}
		});
		Utils.animateListView((ListView) parent, convertView, position);
		return convertView;
	}

	private static class Holder {
		TextView tvName;
		TextView tvDesc;
		ImageView imgMore;
		ImageView imgIcon;
	}

	public void setLisener(OnInnerClickLisener lisener) {
		this.lisener = lisener;
	}

	public interface OnInnerClickLisener {
		void onInnerClickLisener(View view, int position);
	}

}
